<?php  

	/**
	* 职位类别
	*/
	class PosCateModel extends Model
	{
		public $table = 'positioncate';
		// 所有职位
		public $pos = null;
		//顶级职位
		public $topPos = null;
		//二级职位
		public $sonPos = null;
		public function __init()
		{
			$this->pos = S('pos',false,null, array('dir'=>CACHE_PATH));
			$this->topPos = S('topPos',false,null, array('dir'=>CACHE_PATH));
			$this->sonPos = S('sonPos',false,null, array('dir'=>CACHE_PATH));
		}


		public function update_cache()
		{
			$res = $this->all();

			$temp=array();
			foreach ($res as $value) {
				$temp['pos'][$value['posid']] = $value['posname'];
				if ($value['pid'] == 0) {
					$temp['topPos'][$value['posid']] =  $value['posname'];
				}
				if($value['pid']>0){
					$temp['sonPos'][$value['pid']][$value['posid']] = $value['posname'];
				}
			}
			// p($temp);
			S('pos',$temp['pos'],0, array('dir'=>CACHE_PATH));
			S('topPos',$temp['topPos'],0, array('dir'=>CACHE_PATH));
			S('sonPos',$temp['sonPos'],0, array('dir'=>CACHE_PATH));
		}

		/**
		 * 获取职位信息 返回的数组样例 array('顶级职位id'=>array('posname'=>'顶级职位名称'，'son'=>array('二级城市id'=>'二级城市名称')))
		 * @return [type] [description]
		 */
		public function get_pos_data()
		{
			$data = array();
			foreach ($this->sonPos as $key => $value) {
				$data[$key]['posname'] = $this->topPos[$key];
				$data[$key]['son'] = $value;
			}

			return $data;
		}


		public function get_pos($posid)
		{
			$sonpos = $this->sonPos;
			if($posid != 0 ){
				$posArr = array();
				foreach ($sonpos[$posid] as $key => $value) {
					$posArr[] = array(
							'posid' => $key,
							'posname' => $value
						);
				}
			}else{
				$posArr[] = array('posid' => 0,'posname' => '请选择');
			}

			return $posArr;
		}

		public function get_pos_by_id($posid)
		{
			$temp =array();
			#如果id在顶级里，否则在二级里,或者id不存在
			if (array_key_exists($posid, $this->topPos)) {
				$temp['topPos'] = array('posid'=>$posid,'posname'=>$this->topPos[$posid]);
				$temp['sonPos'] = null;
				$temp['sonPosAll'] = $this->get_all_son_by_pid($posid);
				return $temp;
			}
			foreach ($this->sonPos as $key => $value) {
				if(array_key_exists($posid, $value)){
					foreach ($value as $k => $v) {
						if($k == $posid){
							$temp['topPos'] = array('posid'=>$key,'posname'=>$this->topPos[$key]);
							$temp['sonPos'] = array('posid'=>$posid,'posname'=>$v);
							$temp['sonPosAll'] = $this->get_all_son_by_pid($key);
							return $temp;
						}
					}
				}
			}

			return $temp;
		}


		public function get_all_son_by_pid($posid)
		{
			if ($posid === 0) {
				return false;
			}
			return $this->sonPos[$posid];
		}

		public function get_son_by_pid($posid)
		{
			 foreach ($this->sonPos as $key => $value) {
			 	if ($value['posid'] == $posid) {
			 		return $value['posid'];
			 	}
			 }
		}



		public function get_pos_id($pid)
		{
			// p($this->sonCity);
			$temp = array();
			foreach ($this->sonPos as $key => $value) {
				if($key == $pid){
						$temp = array_keys($value);
				}
			}
			$temp[] = $pid;
			return $temp;
		}



		public function get_posnum(){
		//读取缓存
		$posnum = S('posnum',false,0,array('dir'=>CACHE_PATH));
		if(!$posnum){
			$where = array(
				'showstate'		=>	1,//开启状态
				'verifystate'	=>	2,//审核通过
				'endtime'		=>	array('gt'=>time())
			);

			$temp = array();
			$composModel = K('ComPos');
			// p($this->topPos);
			foreach ($this->topPos as $k => $v) {
				//得出每个顶级的子级id
				$posidArr = $this->get_pos_id($k);
				// p($posidArr);
				// p($v);
				$temp[$k]['name'] = $v;
				//统计总数
				$temp[$k]['num'] = $composModel->where($where)->in(array('cate_posid'=>$posidArr))->count();
			}

			//设置缓存
			S('posnum',$temp,30,array('dir'=>CACHE_PATH));
			$posnum = $temp;
		}
		// p($posnum);
	
		return $posnum;
	}
	}
?>